<?php


namespace app\admin\controller;


use app\common\model\User as UserModel;
use think\Controller;
use think\Request;

class UserController extends Controller
{
    protected $loginRquire = true;
    public function _initialize()
    {

        if ($this->loginRquire && session('username')){
            return;
        }

        $this->error('请先登录', url('admin/login/index'), '',2);
    }

    public function index()
    {
        $user = new UserModel();
        $users = $user->select();

        // 向V层传递数据
        $this->assign('users', $users);

        // 取回打包后的数据
        $html = $this->fetch();

        // 返回数据给用户
        return $html;
    }

    public function user_show($id=null)
    {

        if ($id){
            $user_id = $id;
        }else{
            $user_id = $this->request->get('id');
        }

        $user = UserModel::get($user_id);

        if (is_null($user)){
            return json(['code'=>4000, 'message'=>'用户不存在']);
        }

        $this->assign('user', $user);
        $html = $this->fetch();
        return $html;
    }


    public function create()
    {

        if ($this->request->isGet()) {
            $html = $this->fetch('user/create');
            return $html;
        }

        if (!Request::instance()->isAjax())
        {
           return "post wrong";
        }

        // form表单提交逻辑
        $postData = Request::instance()->post();

        $user = UserModel::get(['name' => $postData['name']]);
        if (!is_null($user)) {
            // return $this->error('用户名已存在', '', '', '1');
            return json(['code'=>4000, 'message'=> '用户名已存在']);
        }
        // 用户未在库中，进行创建
        $user = new UserModel();
        $data = [
            'name'=> $postData['name'],
            'phone' => $postData['mobile'],
            'group' => $postData['group'],
            'email' => $postData['email'],
        ];

        if ($user->allowField(true)->validate(true)->save($data)) {
            // return $this->success('添加成功');
            return json(['code'=>0, 'message'=> '添加成功']);
        }
        // return $this->success('添加成功吗', '', '', '1');
        return json(['code'=>4000, 'message'=> $user->getError()]);
    }


    public function delete()
    {
        $user_id = Request::instance()->get('id');
        $user = UserModel::get($user_id);

        if (is_null($user)) {
            // return $this->error("该用户id: $user_id 不存在" );
            return json(['code' => 4000, 'message' => "该用户id: $user_id 不存在"]);
        }
        if ($user->delete()) {
            return json(['code' => 0, 'message' => "删除成功"]);
        }

        return json(['code'=> 4000, 'message' => '删除失败:'.$user->getError()]);

    }


    public function update()
    {
        if ($this->request->isGet()) {
            $user_id = Request::instance()->get('id');
            $user = UserModel::get($user_id);
            if (is_null($user)){
                return $this->error('用户不存在');
            }

            $this->assign('user', $user);
            $html = $this->fetch('user/update');
            return $html;
        }

        // method为post
        $user = new UserModel();
        $postData = Request::instance()->post();
        $data = [
            'name'=> $postData['name'],
            'phone' => $postData['mobile'],
            'group' => $postData['group'],
            'email' => $postData['email'],
        ];

        if($user->allowField(true)->isUpdate(true)->save($data, ['id'=>$postData['id']]))
        {
            return json(['code'=>0, 'message'=> '更新成功']);
        }
        return json(['code'=>4000, 'message'=> '更新失败:'.$user->getError()]);
    }


    public function demo($uid)
    {
        $user = UserModel::get($uid);
        $user_projects = $user->projects;
        // var_dump($user_projects);
        // echo $user->user_project_count();
        $this->assign('projects', $user_projects);
        // dump($user_projects);
        return $this->fetch('project/index');

    }
}